import Vue from 'vue'
import VueRouter from 'vue-router'
// 登陆路由
import login from '../components/user/login'
import home from '../views/home'
// home子路由
import welcome from '../components/user/welcome'
import users from '../components/user/users'
import roles from '../components/power/roles'
import rights from '../components/power/rights'
import cate from '../components/goods/cate'
import params from '../components/goods/params'
import goodsList from '../components/goods/list'
import addGoods from '../components/goods/addGoods'
import order from '../components/order/order'
import chart from '../components/report/chart'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: login
  },
  {
    path: "/login",
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect:'/welcome',
    children: [
      {
        path: '/welcome',
        component: welcome
      },
      {
        path: '/users',
        component: users
      },
      {
        path:'/roles',
        component:roles
      },{
        path:'/rights',
        component:rights
      },{
        path:'/categories',
        component:cate
      },
      {
        path:'/params',
        component:params
      },{
        path:'/goods',
        component:goodsList
      },
      {
        path:'/addGoods',
        component:addGoods
      },{
        path:'/orders',
        component:order
      },{
        path:'/reports',
        component:chart
      }
    ]
  }
  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Home
  // },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from代表从哪个路径跳转而来
  // next()是一个函数,表示放行
  // next() 放行 next('/login) 强制跳转
  if (to.path == '/' || to.path == '/login') return next()
  let token = window.sessionStorage.getItem('token')
  if (!token) return next('/')
  next()
})

export default router
